package com.teen9g.infoplatformserver.task;

import com.teen9g.constant.RedisKey;
import com.teen9g.entity.CanteenShop;
import com.teen9g.entity.MaimaiChunithm;
import com.teen9g.entity.Playground;
import com.teen9g.entity.TeachingBuilding;
import com.teen9g.infoplatformserver.mapper.CanteenShopMapper;
import com.teen9g.infoplatformserver.mapper.MaimaiChunithmMapper;
import com.teen9g.infoplatformserver.mapper.PlaygroundMapper;
import com.teen9g.infoplatformserver.mapper.TeachingBuildingMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Set;

@Slf4j
@Component
public class ShopViewSyncTask {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private CanteenShopMapper canteenShopMapper;

    @Autowired
    private TeachingBuildingMapper teachingBuildingMapper;

    @Autowired
    private PlaygroundMapper playgroundMapper;

    @Autowired
    private MaimaiChunithmMapper maimaiChunithmMapper;

    @Scheduled(cron = "0 0/5 * * * *")
    public void syncAllShopViews() {
        log.info("每5分钟同步店铺浏览量");
        Set<String> keys = redisTemplate.keys(RedisKey.CS_VIEW_KEY + "*");
        for (String key : keys) {
            // 提取店铺ID
            Integer shopId = Integer.valueOf(key.substring(RedisKey.CS_VIEW_KEY.length()));
            String viewCountStr = redisTemplate.opsForValue().get(key);
            System.out.println("店铺ID: " + shopId + ", 浏览量: " + viewCountStr);
            if (viewCountStr != null) {
                Integer viewCount = Integer.valueOf(viewCountStr);
                //同步到MySQL
                canteenShopMapper.updateViewCount(shopId, viewCount);
                //删除Redis中的数据
                redisTemplate.delete(key);
            }

        }
    }

    @Scheduled(cron = "0 0/5 * * * *")
    public void syncAllTeachingBuildingsViews() {
        log.info("每5分钟同步教学楼浏览量");
        Set<String> keys = redisTemplate.keys(RedisKey.TB_VIEW_KEY + "*");
        for (String key : keys) {
            // 提取ID
            Integer contentId = Integer.valueOf(key.substring(RedisKey.TB_VIEW_KEY.length()));
            String viewCountStr = redisTemplate.opsForValue().get(key);
            System.out.println("条目ID: " + contentId + ", 浏览量: " + viewCountStr);
            if (viewCountStr != null) {
                Integer viewCount = Integer.valueOf(viewCountStr);
                //同步到MySQL
                teachingBuildingMapper.updateViewCount(contentId, viewCount);
                //删除Redis中的数据
                redisTemplate.delete(key);
            }
        }
    }

    @Scheduled(cron = "0 0/5 * * * *")
    public void syncAllPlaygroundViews() {
        log.info("每5分钟同步运动场浏览量");
        Set<String> keys = redisTemplate.keys(RedisKey.PL_VIEW_KEY + "*");
        for (String key : keys) {
            // 提取ID
            Integer contentId = Integer.valueOf(key.substring(RedisKey.PL_VIEW_KEY.length()));
            String viewCountStr = redisTemplate.opsForValue().get(key);
            System.out.println("条目ID: " + contentId + ", 浏览量: " + viewCountStr);
            if (viewCountStr != null) {
                Integer viewCount = Integer.valueOf(viewCountStr);
                //同步到MySQL
                playgroundMapper.updateViewCount(contentId, viewCount);
                //删除Redis中的数据
                redisTemplate.delete(key);
            }
        }
    }

    @Scheduled(cron = "0 0/5 * * * *")
    public void syncAllMaimaiChunithmViews() {
        log.info("每5分钟同步舞萌中二浏览量");
        Set<String> keys = redisTemplate.keys(RedisKey.MC_VIEW_KEY + "*");
        for (String key : keys) {
            // 提取ID
            Integer contentId = Integer.valueOf(key.substring(RedisKey.MC_VIEW_KEY.length()));
            String viewCountStr = redisTemplate.opsForValue().get(key);
            System.out.println("条目ID: " + contentId + ", 浏览量: " + viewCountStr);
            if (viewCountStr != null) {
                Integer viewCount = Integer.valueOf(viewCountStr);
                //同步到MySQL
                maimaiChunithmMapper.updateViewCount(contentId, viewCount);
                //删除Redis中的数据
                redisTemplate.delete(key);
            }
        }
    }
    @Scheduled(cron = "0 0 * * * *")
    public void syncTop10TotalViewCanteenShops() {
        log.info("每小时同步总浏览量前10的店铺信息");
        // MySQL获取总浏览量前10的店铺ID
        List<CanteenShop> list = canteenShopMapper.selectTopTotalViewCanteenShops(10);

        if (list == null || list.isEmpty()) {
            log.info("获取前十浏览量的店铺信息失败");
            return;
        }

        //删除Redis中的数据
        redisTemplate.delete(RedisKey.CS_TOP10_KEY);

        //将新数据存入Redis
        list.forEach(canteenShop -> {
            String triple = String.format(
                    "%d|%s|%d",
                    canteenShop.getShopId(),
                    canteenShop.getShopName(),
                    canteenShop.getShopView()
            );
            redisTemplate.opsForZSet()
                    .add(RedisKey.CS_TOP10_KEY,
                            triple,
                            canteenShop.getShopView());
        });
    }

    @Scheduled(cron = "0 0 * * * *")
    public void syncTop10TotalViewTeachingBuildings() {

        log.info("每小时同步总浏览量前10的教学楼信息");
        // MySQL获取总浏览量前10的教学楼
        List<TeachingBuilding> list = teachingBuildingMapper.selectTopTotalViewTeachingBuildings(10);
        if (list == null || list.isEmpty()) {
            log.warn("获取前十浏览量的教学楼失败");
            return;
        }
        //删除Redis中的数据
        redisTemplate.delete(RedisKey.TB_TOP10_KEY);

        //将新数据存入Redis
        list.forEach(teachingBuilding -> {
            String triple = String.format(
                    "%d|%s|%d",
                    teachingBuilding.getBuildingId(),
                    teachingBuilding.getContentTitle(),
                    teachingBuilding.getContentView()
            );
            redisTemplate.opsForZSet().
                    add(RedisKey.TB_TOP10_KEY,
                            triple,
                            teachingBuilding.getContentView()); //使用浏览量作为排序分数
        });
        log.info("同步总浏览量前10的教学楼信息成功");
    }

    @Scheduled(cron = "0 0 * * * *")
    public void syncTop10TotalViewPlaygrounds() {
        log.info("每小时同步总浏览量前10的运动场信息");
        // MySQL获取总浏览量前10的运动场
        List<Playground> list = playgroundMapper.selectTopTotalViewPlaygrounds(10);
        if (list == null || list.isEmpty()) {
            log.warn("获取前十浏览量的运动场失败");
            return;
        }
        //删除Redis中的数据
        redisTemplate.delete(RedisKey.PL_TOP10_KEY);
        //将新数据存入Redis
        list.forEach(playground -> {
            String triple = String.format(
                    "%d|%s|%d",
                    playground.getContentId(),
                    playground.getContentTitle(),
                    playground.getPlaygroundView()
            );
            redisTemplate.opsForZSet().
                    add(RedisKey.PL_TOP10_KEY,
                            triple,
                            playground.getPlaygroundView()); //使用浏览量作为排序分数
        });
    }

    @Scheduled(cron = "0 0 * * * *")
    public void syncTop10TotalViewMaimaiChunithms() {
        log.info("每小时同步总浏览量前10的舞萌中二信息");
        // MySQL获取总浏览量前10的舞萌中二
        List<MaimaiChunithm> list = maimaiChunithmMapper.selectTopTotalViewMaimaiChunithms(10);
        if (list == null || list.isEmpty()) {
            log.warn("获取前十浏览量的舞萌中二失败");
            return;
        }
        //删除Redis中的数据
        redisTemplate.delete(RedisKey.MC_TOP10_KEY);
        list.forEach(maimaiChunithm -> {
            String triple = String.format(
                    "%d|%s|%d",
                    maimaiChunithm.getContentId(),
                    maimaiChunithm.getShopName(),
                    maimaiChunithm.getContentView()
            );
            redisTemplate.opsForZSet().
                    add(RedisKey.MC_TOP10_KEY,
                            triple,
                            maimaiChunithm.getContentView()); //使用浏览量作为排序分数
        });
    }
}
